package com.sfy.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sfy.auth.application.convert.RolePermissionDTOConverter;
import com.sfy.auth.application.dto.AuthRolePermissionDTO;
import com.sfy.auth.common.entity.Result;
import com.sfy.auth.domain.entity.AuthRolePermissionBO;
import com.sfy.auth.domain.service.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/rolePermission/")
@Slf4j
public class RolePermissionController {

    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    /**
     * 新增角色权限
     *
     * @param
     * @return
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthRolePermissionDTO authRolePermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RolePermissionController:add:{}", JSON.toJSONString(authRolePermissionDTO));
            }
            Preconditions.checkArgument(!CollectionUtils.isEmpty(authRolePermissionDTO.getPermissionIdList())
                    , "权限列表不能为空");
            Preconditions.checkNotNull(authRolePermissionDTO.getRoleId(),
                    "角色id不能为空");
            AuthRolePermissionBO authRolePermissionBO = RolePermissionDTOConverter
                    .INSTANCE.authRolePermissionDTOToAuthRolePermissionBO(authRolePermissionDTO);
            Boolean add = authRolePermissionDomainService.add(authRolePermissionBO);
            return Result.OK(add);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.Fail(e);
        }
    }


}
